package com.hkts.management.orderimg.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.imgrecord.entity.HktsOrderImgrecord;
import com.hkts.management.imgrecord.service.IHktsOrderImgrecordService;
import com.hkts.management.orderimg.entity.HktsOrderImg;
import com.hkts.management.orderimg.mapper.HktsOrderImgMapper;
import com.hkts.management.orderimg.service.IHktsOrderImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2021-04-22
 */
@Service
@Transactional
public class HktsOrderImgServiceImpl extends ServiceImpl<HktsOrderImgMapper, HktsOrderImg> implements IHktsOrderImgService {


    @Autowired
    private HktsOrderImgMapper hktsOrderImgMapper;
    @Autowired
    private IHktsOrderImgrecordService orderImgrecordService;

    @Override
    public IPage<HktsOrderImg> selectPage(Page<HktsOrderImg> page, HktsOrderImg hktsOrderImg) {
        QueryWrapper<HktsOrderImg> query = Wrappers.query();
        QueryWrapper<HktsOrderImg> entityWrapper = getEntityWrapper(query, hktsOrderImg);
        return hktsOrderImgMapper.selectPage(page, entityWrapper);
    }


    @Override
    public Map<String, Object> selectList(String orderId,Integer type) {
        QueryWrapper<HktsOrderImg> query = Wrappers.query();
        query.lambda().eq(!orderId.isEmpty(), HktsOrderImg::getOrderId, orderId)
                .eq(type != null, HktsOrderImg::getType,type)
        .eq(HktsOrderImg::getIsDel,0);
        List<HktsOrderImg> hktsOrderImgs = hktsOrderImgMapper.selectList(query);
        Map<String, Object> result = new HashMap<>();
        List<Object> loding = new ArrayList<>();
        List<Object> unLoding = new ArrayList<>();
        List<Object> receipt = new ArrayList<>();
        for (int i = 0; i < hktsOrderImgs.size(); i++) {

            if (hktsOrderImgs.get(i).getType() == 0) {

                loding.add(hktsOrderImgs.get(i).getImgUrl());
                result.put("loding", loding);
            } else if (hktsOrderImgs.get(i).getType() == 1) {

                unLoding.add(hktsOrderImgs.get(i).getImgUrl());
                result.put("unLoding", unLoding);
            } else {

                receipt.add(hktsOrderImgs.get(i).getImgUrl());
                result.put("receipt", receipt);
            }
        }
        List<HktsOrderImgrecord> hktsOrderImgrecords=orderImgrecordService.getHktsOrderImgrecordByOrderId(orderId);
        result.put("orderImgrecord",hktsOrderImgrecords);
        return result;
    }

    @Override
    public Map<String, Object> getOrderImgByRecordId(String recordId) {
        QueryWrapper<HktsOrderImg> query = Wrappers.query();
        query.lambda().eq( HktsOrderImg::getRecordId, recordId);
        List<HktsOrderImg> hktsOrderImgs = hktsOrderImgMapper.selectList(query);
        Map<String, Object> result = new HashMap<>();
        List<Object> loding = new ArrayList<>();
        List<Object> unLoding = new ArrayList<>();
        List<Object> receipt = new ArrayList<>();
        for (int i = 0; i < hktsOrderImgs.size(); i++) {

            if (hktsOrderImgs.get(i).getType() == 0) {

                loding.add(hktsOrderImgs.get(i).getImgUrl());
                result.put("loding", loding);
            } else if (hktsOrderImgs.get(i).getType() == 1) {

                unLoding.add(hktsOrderImgs.get(i).getImgUrl());
                result.put("unLoding", unLoding);
            } else {

                receipt.add(hktsOrderImgs.get(i).getImgUrl());
                result.put("receipt", receipt);
            }
        }
        return result;
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsOrderImg> getEntityWrapper(QueryWrapper<HktsOrderImg> entityWrapper, HktsOrderImg hktsOrderImg) {
        //条件拼接
        return entityWrapper;
    }
}
